package com.medvalley.tcm.service.impl;

import com.google.common.collect.Lists;
import com.medvalley.tcm.base.BusinessException;
import com.medvalley.tcm.dao.SickInfoDao;
import com.medvalley.tcm.dao.SickInfoStorageDao;
import com.medvalley.tcm.domain.SickInfo;
import com.medvalley.tcm.domain.SickInfoStorage;
import com.medvalley.tcm.service.SickInfoStorageService;
import com.medvalley.tcm.vo.inputvo.SickInfoStorageInputVo;
import com.medvalley.tcm.vo.outputvo.SickInfoStorageOutputVo;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author linsong
 * @date 2019/7/21 11:13
 **/
@Service
public class SickInfoStorageServiceImpl implements SickInfoStorageService {

    @Autowired
    private SickInfoStorageDao sickInfoStorageDao;

    @Autowired
    private SickInfoDao sickInfoDao;

    @Override
    public void save(SickInfoStorageInputVo sickInfoStorageInputVo) {
        String barCode = sickInfoStorageInputVo.getBarcode();
        String phone = sickInfoStorageInputVo.getPhone();
        SickInfo sickInfo = sickInfoDao.findOneByBarCode(barCode);
        if (sickInfo == null || sickInfo.getIsPrint() == 0) {
            throw new BusinessException("病历尚未完成");
        }
        SickInfoStorage sickInfoStorage = sickInfoStorageDao.findOneByBarCodeAndPhone(barCode, phone);
        if (sickInfoStorage != null) {
            throw new BusinessException("已经保存过这份报告，不要重复保存");
        }
        sickInfoStorage = new SickInfoStorage();
        sickInfoStorage.setBarCode(sickInfoStorageInputVo.getBarcode());
        sickInfoStorage.setPhone(sickInfoStorageInputVo.getPhone());
        sickInfoStorage.setFilePath(sickInfoStorageInputVo.getFilePath());
        sickInfoStorage.setPatientName(sickInfo.getPatientName());
        sickInfoStorage.setPatientAge(sickInfo.getPatientAge());
        sickInfoStorage.setPatientSex(sickInfo.getPatientSex());
        sickInfoStorage.setCreateTime(new Date());
        sickInfoStorageDao.save(sickInfoStorage);
    }

    @Override
    public List<SickInfoStorageOutputVo> listByPhone(String phone) {
        List<SickInfoStorageOutputVo> sickInfoStorageOutputVoList = Lists.newArrayList();
        List<SickInfoStorage> sickInfoStorages = sickInfoStorageDao.findAllByPhoneOrderByIdDesc(phone);
        if (!CollectionUtils.isEmpty(sickInfoStorages)) {
            sickInfoStorages.forEach(ss -> {
                SickInfoStorageOutputVo sickInfoStorageOutputVo = new SickInfoStorageOutputVo();
                sickInfoStorageOutputVo.setPhone(ss.getPhone());
                sickInfoStorageOutputVo.setBarcode(ss.getBarCode());
                sickInfoStorageOutputVo.setPatientName(ss.getPatientName());
                sickInfoStorageOutputVo.setPatientAge(ss.getPatientAge());
                sickInfoStorageOutputVo.setPatientSex(ss.getPatientSex());
                sickInfoStorageOutputVo.setDownloadPath(ss.getFilePath());
                sickInfoStorageOutputVo.setCreateTime(DateFormatUtils.format(ss.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                sickInfoStorageOutputVoList.add(sickInfoStorageOutputVo);
            });
        }
        return sickInfoStorageOutputVoList;
    }
}
